<?php

    /**
     * Action
     *
     * @package      Core
     * @subpackage   Selector
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
	class Core_Selector_Action extends Core_Selector_Abstract
	{

    ////////////////////////////////////////////////////////////////////////////
	//                                Variables                               //
	////////////////////////////////////////////////////////////////////////////

	    /**
         * Name of the controller in the module
         *
         * @var string
         */
        protected $_controller	= '';

	    /**
         * Name of the method in the controller
         *
         * @var string
         */
        protected $_method  = '';

	    /**
         * The request type code
         *
         * @var string
         */
        protected $_requestType = '';

	////////////////////////////////////////////////////////////////////////////
	//                            Getters / Setters                           //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Returns the class name
         *
         * @return      string
         */
	    public function getClass()
		{
            return ucfirst($this->_module) . '_Controllers_'. ucfirst($this->_registry['end']) . '_' . $this->_controller;
	    }

        /**
         * Returns the name of the controller in the module
         *
         * @return      string
         */
	    public function getController()
		{
	        return $this->_controller;
	    }

		/**
         * Returns the method of the controller
         *
         * @return      string
         */
        public function getMethod()
		{
			return $this->_method;
		}

		/**
         * Sets the method of the controller
         *
         * @param		string		$value
		 * @throws		Core_ArgumentException
		 * @throws		Core_ArgumentNullException
         * @return      Core_Selector_Action
         */
        public function setMethod($value)
		{
            if ($value === null) {
				throw new Core_ArgumentNullException('value is a null reference');
			}

            if (strlen(trim($value)) == 0) {
				throw new Core_ArgumentException('value is a zero-length string');
			}

			$this->_method = $value;
            return $this;
		}

		/**
         * Returns the request type code
         *
         * @return      string
         */
        public function getRequestType()
		{
			return $this->_requestType;
		}

	////////////////////////////////////////////////////////////////////////////
	//                                 Methods                                //
	////////////////////////////////////////////////////////////////////////////

	    /**
		 * Constructor
		 *
		 * @param       string      $requestType
		 * @param       string      $module
		 * @param       string      $action
		 * @throws		Core_ArgumentException
		 * @throws		Core_ArgumentNullException
	     */
	    public function __construct($requestType, $module, $action)
		{
            if ($requestType === null) {
				throw new Core_ArgumentNullException('requestType is a null reference');
			}

            if (strlen(trim($requestType)) == 0) {
				throw new Core_ArgumentException('requestType is a zero-length string');
			}

            if ($module === null) {
				throw new Core_ArgumentNullException('module is a null reference');
			}

            if (strlen(trim($module)) == 0) {
				throw new Core_ArgumentException('module is a zero-length string');
			}

            if ($action === null) {
				throw new Core_ArgumentNullException('action is a null reference');
			}

            if (strlen(trim($action)) == 0) {
				throw new Core_ArgumentException('action is a zero-length string');
			}

	        $this->_module   = $module;
            $this->_registry = Zend_Registry::getInstance();

	        $r = explode(':', $action);
	        if (count($r) == 1) {
	            $this->_controller = 'Index';
	            $this->_method = (empty($r[0])) ? 'index' : $r[0];
	        } else {
	            $this->_method = (empty($r[1])) ? 'index' : $r[1];
	            $this->_controller = (empty($r[0])) ? 'Index' : ucfirst(strtolower($r[0]));
	        }

            $this->_resource = $this->_controller .':'. $this->_method;
            $this->_requestType = $requestType;
            $this->_createPath();
	    }

	////////////////////////////////////////////////////////////////////////////
	//                            Protected methods                           //
	////////////////////////////////////////////////////////////////////////////

	    /**
         * Sets the path to the module controller class
         *
         * @return      void
         */
        protected function _createPath()
		{
			$path = Core_IO_Path::combine(
                                    PROJECT_MODULE_PATH,
                                    ucfirst(strtolower($this->_module)),
                                    'controllers',
                                    $this->_registry['end'],
                                    $this->_controller . 'Controller.php'
                                );

            if (!Core_IO_File::exists($path)) {
                $path = Core_IO_Path::combine(
                            SERUM_MODULE_PATH,
                            ucfirst(strtolower($this->_module)),
                            'controllers',
                            $this->_registry['end'],
                            $this->_controller . 'Controller.php'
                        );
            }

            $this->_path = $path;
	    }
	}
